package com.qf.utils;

import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;

import java.util.Map;
import java.util.Random;

/**
 * 2019/11/22
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */

public class YunPian {

    //发送验证码
    public static String yunPian(String phoneCode){
        String code="a4e158a3b965517680d6fc98726b803b";
        //初始化clnt,使用单例方式
        YunpianClient clnt = new YunpianClient(code).init();
        //发送短信API
        Map<String, String> param = clnt.newParam(2);

        //生成随机的6位数验证码
        String identifyingCode = getNumber(6);
        param.put(YunpianClient.MOBILE, phoneCode);
        param.put(YunpianClient.TEXT, "【鹏泊科技】您的验证码是"+identifyingCode);

        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()

        //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*

        //释放clnt
        clnt.close();
        return identifyingCode;
    }

    //生成6位验证码
    public static  String getNumber(int length){

        String source = "0123456789";
        Random r = new Random();
        StringBuilder number = new StringBuilder();
        for (int j = 0; j < length; j++) {
            number.append(source.charAt(r.nextInt(10)));
        }
        return number.toString();
    }
}
